# -*- coding: utf-8 -*-

__author__ = 'bilibili'

import types
import copy
from spot_excel_reader import spot_excel_reader
from spot_config import spot_config

class spot_AM_Report_analyzer:
    # FUNCTION: __init__(self, string)
    #   the constructor of the class spot_AM_Report_analyzer
    # ARGS:
    #   @config_name: json config file name
    def __init__(self, config_name='AM_Report.config.json'):
        self.config = spot_config(config_name)
        self.excel = spot_excel_reader(self.config.get_attr('config', 'excel_file_name'), self.config.get_attr('config', 'excel_sheet_name'))
        self.res_array = []
        self.area_dict = {}
        self.attr_1 = self.config.get_attr('config', 'attr_row_1')
        self.attr_2 = self.config.get_attr('config', 'attr_row_2')
#        self.config.debug_output()

    # FUNCTION: build_entry(self, int)
    #   to convert one row in the excel into a dictionary
    # ARGS:
    #   @row_num: the row number to needs to be converted
    # RETURN:
    #   dict: the dictionary that is converted from one line of the excel
    def build_entry(self, row_num):
        tmp_dict = copy.deepcopy(self.config.get_dict())
        del(tmp_dict['config'])
        for (d, x) in self.config.get_dict().items():
            if d == 'config':
                continue
            if type(x) == types.IntType:
                tmp_dict[d] = self.excel.get_cube(row_num, x)
            else: # it's a dictionary
                for (d2, x2) in x.items():
                    tmp_dict[d][d2] = self.excel.get_cube(row_num, x2)
        return tmp_dict

    # FUNCTION: build_whole_table(self,)
    #   to convert the whole table into dictionaries
    def build_whole_table(self):
        self.res_array = []
        for i in range(self.config.get_attr('config', 'attr_row_2') + 1, self.excel.get_row_count()):
            tmp_dict = self.build_entry(i)
            self.res_array.append(tmp_dict)
            if not self.area_dict.has_key(tmp_dict['Area']):
                self.area_dict[tmp_dict['Area']] = []
            self.area_dict[tmp_dict['Area']].append(tmp_dict)

    # FUNCTION: get_result_array(self, string)
    #   to get the dictionaries from the excel
    # ARGS:
    #   @area: the area string that is specified to get the ared info
    # RETURN:
    #   list: the dictionaries list that contains the content from the excel
    def get_result_array(self, area=None):
        if area == None:
            return self.res_array
        return self.area_dict[area]

    # FUNCTION: get_area_dict(self)
    #   to get the area dictionary
    # RETURN:
    #   dict: area dictionary
    def get_area_dict(self):
        return self.area_dict

def test_spot_AM_Report_analyzer():
    analyzer = spot_AM_Report_analyzer()
    analyzer.build_whole_table()
    for i in analyzer.get_area_dict()['Canada']:
        print i
    print len(analyzer.get_area_dict()['Canada'])

if __name__ == '__main__':
    test_spot_AM_Report_analyzer()